<?php
    
	include_once "include/constants.php";
	
	$ownerid = $session->userinfo['id'];

    try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
         
        $stmt = $conn->prepare("select mn.id as movienightid, mn.date, u.id as userid, concat(u.firstname, ' ', u.lastname) as fullname, m.movieid, m.title, m.year, m.imdbid
                                from movienights mn, movienightstousers mn2u, users u, movies m
                                where mn.id = mn2u.movienightid
                                and mn2u.userid = u.id
                                and mn.movieid = m.movieid
                                and mn.id in (select distinct movienightid from movienightstousers where userid = :ownerid)
                                and mn.date >= subdate(now(), 1)
                                order by date, movienightid;");
        $stmt->bindValue(':ownerid', (int) $ownerid, PDO::PARAM_INT);
        $stmt->execute();
        
        $events = array();
        while($row = $stmt->fetch()) {
            if (!isset($events[$row['movienightid']])) {
                $events[$row['movienightid']] = array();
            }
            $events[$row['movienightid']][] = $row;
        }

        echo "<script>ownerId = " . $session->userinfo['id'] . "</script>";
		
		$stmt = $conn->prepare("select * from notifications where userid = :ownerid and active = 1;");
		$stmt->bindValue(':ownerid', (int) $ownerid, PDO::PARAM_INT);
        $stmt->execute();
		
		$new_events = array();
		while($row = $stmt->fetch()) {
            $new_events[$row['movienightid']] = 1;
        }
		
		$stmt = $conn->prepare("update notifications set active = 0 where userid = :ownerid and active = 1;");
		$stmt->bindValue(':ownerid', (int) $ownerid, PDO::PARAM_INT);
        $stmt->execute();
        
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>

<div class="container margin-top-bottom">
	<div class="row">
		<div class="span3">
            <?php
                define('CURR_PAGE', 'events');
                include 'mn_sidebar.php';
            ?>
        </div>
		<div class="span9">
			<div class="page-header">
				<h1>Events <small>Under construction</small></h1>
			</div>
            <div>
                <?php foreach($events as $event): ?>
				<?php
					$cls = "";
					if (isset($new_events[$event[0]['movienightid']])) {
						$cls = "mn_highlight";
					}
				?>
                <div class="row <?php echo $cls; ?>" style="border-bottom: 1px #eee solid; padding: 3px 0;">
                    <div class="span1" style="text-align: center; margin-bottom: 8px;">
                        <img width="55px" height="75px" src="img/posters/<?php echo $event[0]['imdbid']; ?>_small.jpg" alt="poster">
                    </div>
                    <div class="span4">
                        <h4><?php
                            $mndate = strtotime($event[0]['date']);
                            echo date("l, d/m/Y", $mndate);
                        ?></h4>
                        <?php echo $event[0]['title']; ?>
                        <ul>
                        <?php foreach($event as $member): ?>
                            <li><?php echo $member['fullname']; ?></li>
                        <?php endforeach; ?>
                        </ul>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
		</div>
	</div>
</div>